package com.tbit.main.service.impl;

import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.dao.core.MemberLogDao;
import com.tbit.main.dao.core.RideCardDao;
import com.tbit.main.dao.core.RideLogDao;
import com.tbit.main.dao.core.UserDao;
import com.tbit.main.pojo.*;
import com.tbit.main.service.*;
import com.tbit.main.util.DateTimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 代理商数据统计
 *
 * @author Leon
 * 2019年11月13日 下午10:22:54
 */
@Service("agentStaService")
public class AgentStaServiceImpl implements AgentStaService {
    /**
     * 金额格式化
     */
    private DecimalFormat df = new DecimalFormat("0.00");
    @Autowired
    private UserService userService;
    @Autowired
    private MachineService machineService;
    @Autowired
    private UserDao userDao;
    @Autowired
    private RideLogDao rideLogDao;
    @Autowired
    private MemberLogDao memberLogDao;
    @Autowired
    private RideCardDao rideCardDao;
    @Autowired
    private RideCardService rideCardService;
    @Autowired
    private RideLogService rideLogService;
    @Autowired
    private MemberLogService memberLogService;
    @Autowired
    private DispatchLogService dispatchLogService;
    @Autowired
    private VipLogService vipLogService;

    @Override
    public AgentSta staByTime(Integer accountId, String startTime, String endTime) {
        AgentSta agentSta = new AgentSta();
        agentSta.setAccountId(accountId);

        /**设备总数*/
        Integer machineCount = machineService.getCountByAccountId(accountId);
        agentSta.setMachineCount(machineCount);

        /**用户总数*/
        Integer userCount = userService.getUserCount(accountId);
        agentSta.setUserCount(userCount);

        /**免押会员总数*/
        Integer memberUserCount = userService.getDepositUserCount(accountId, DateTimeUtil.getNowTimeYMD());
        agentSta.setMemberUserCount(memberUserCount);

        /**订单信息*/
        RideSta rideSta = rideLogService.staByTime(accountId, startTime, endTime);
        agentSta.setOrderCount(rideSta.getOrderCount());
        if (rideSta.getOrderMoney() == null) {
            agentSta.setOrderMoney("0");
        } else {
            agentSta.setOrderMoney(df.format(rideSta.getOrderMoney() / 100));
        }

        /**订单未支付信息*/
        RideSta rideStaNotPay = rideLogService.staNotPayByTime(accountId, startTime, endTime);
        agentSta.setOrderNotPayCount(rideStaNotPay.getOrderCount());
        if (rideStaNotPay.getOrderMoney() == null) {
            agentSta.setOrderNotPayMoney("0");
        } else {
            agentSta.setOrderNotPayMoney(df.format(rideStaNotPay.getOrderMoney() / 100));
        }

        /**订单补交统计*/
        RideSta rideStaAfterPay = rideLogService.staAfterPayByTime(accountId, startTime, endTime);
        agentSta.setOrderAfterPayCount(rideStaAfterPay.getOrderCount());
        if (rideStaAfterPay.getOrderMoney() == null) {
            agentSta.setOrderAfterPayMoney("0");
        } else {
            agentSta.setOrderAfterPayMoney(df.format(rideStaAfterPay.getOrderMoney() / 100));
        }

        /**免押会员信息*/
        MemberSta memberSta = memberLogService.staByTime(accountId, startTime, endTime);
        agentSta.setMemberOrderCount(memberSta.getMemberOrderCount());
        if (memberSta.getMemberOrderMoney() == null) {
            agentSta.setMemberOrderMoney("0");
        } else {
            agentSta.setMemberOrderMoney(df.format(memberSta.getMemberOrderMoney() / 100));
        }

        /**调度订单和费用*/
        DispatchPaySta dispatchPaySta = dispatchLogService.staByTime(accountId, startTime, endTime);
        agentSta.setDispatchCount(dispatchPaySta.getDispatchCount());
        if (dispatchPaySta.getDispatchMoney() == null) {
            agentSta.setDispatchMoney("0");
        } else {
            agentSta.setDispatchMoney(df.format(dispatchPaySta.getDispatchMoney() / 100));
        }

        /**骑行会员信息*/
        RideCardSta rideCardSta = rideCardService.staByTime(accountId, startTime, endTime);
        agentSta.setRideCardOrderCount(rideCardSta.getRideCardOrderCount());
        if (rideCardSta.getRideCardOrderMoney() == null) {
            agentSta.setRideCardOrderMoney("0");
        } else {
            agentSta.setRideCardOrderMoney(df.format(rideCardSta.getRideCardOrderMoney() / 100));
        }

        /**vip会员*/
        VipSta vipSta = vipLogService.staByTime(accountId, startTime, endTime);
        agentSta.setVipOrderCount(vipSta.getVipOrderCount());
        if (vipSta.getVipOrderMoney() == null) {
            agentSta.setVipOrderMoney("0");
        } else {
            agentSta.setVipOrderMoney(df.format(vipSta.getVipOrderMoney() / 100));
        }

        return agentSta;
    }

    @Override
    public List<KVEntity> staRideTime(Integer accountId, String startTime, String endTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put(DaoParamConstant.startTime, startTime);
        params.put(DaoParamConstant.endTime, endTime);

        return rideLogDao.staRideTime(params);
    }

    @Override
    public List<KVEntity> orderCountGroupByDate(Integer accountId, String startTime, String endTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put(DaoParamConstant.startTime, startTime);
        params.put(DaoParamConstant.endTime, endTime);

        return rideLogDao.orderCountGroupByDate(params);
    }

    @Override
    public List<KVEntity> orderMoneyGroupByDate(Integer accountId, String startTime, String endTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put(DaoParamConstant.startTime, startTime);
        params.put(DaoParamConstant.endTime, endTime);

        return rideLogDao.orderMoneyGroupByDate(params);
    }

    @Override
    public List<KVEntity> memberCountGroupByDate(Integer accountId, String startTime, String endTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put(DaoParamConstant.startTime, startTime);
        params.put(DaoParamConstant.endTime, endTime);

        return memberLogDao.memberCountGroupByDate(params);
    }

    @Override
    public List<KVEntity> memberMoneyGroupByDate(Integer accountId, String startTime, String endTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put(DaoParamConstant.startTime, startTime);
        params.put(DaoParamConstant.endTime, endTime);

        return memberLogDao.memberMoneyGroupByDate(params);
    }

    @Override
    public List<KVEntity> rideCardCountGroupByDate(Integer accountId, String startTime, String endTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put(DaoParamConstant.startTime, startTime);
        params.put(DaoParamConstant.endTime, endTime);

        return rideCardDao.rideCardCountGroupByDate(params);
    }

    @Override
    public List<KVEntity> rideCardMoneyGroupByDate(Integer accountId, String startTime, String endTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put(DaoParamConstant.startTime, startTime);
        params.put(DaoParamConstant.endTime, endTime);

        return rideCardDao.rideCardMoneyGroupByDate(params);
    }

    @Override
    public List<KVEntity> newUserGroupByDate(Integer accountId, String startTime, String endTime) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(DaoParamConstant.accountId, accountId);
        params.put(DaoParamConstant.startTime, startTime);
        params.put(DaoParamConstant.endTime, endTime);

        return userDao.newUserGroupByDate(params);
    }

    @Override
    public List<KVEntity> orderMachineRatio(Integer accountId, String startTime, String endTime) {
        Map<String, Object> params = new HashMap<>(8);
        params.put(DaoParamConstant.accountId, accountId);
        params.put(DaoParamConstant.startTime, startTime);
        params.put(DaoParamConstant.endTime, endTime);

        List<KVEntity> kvEntitys = rideLogDao.orderCountGroupByDate(params);


        /**查询设备总数*/
        Integer machineCount = machineService.getCountByAccountId(accountId);

        for (KVEntity kv : kvEntitys) {
            kv.setV(df.format(Double.parseDouble(kv.getV()) / machineCount));
        }

        return kvEntitys;
    }

}
